<?php
class Answer_Component_Ajax_Ajax extends Phpfox_Ajax
{
    public function vote()
    {
        Phpfox::isUser(true);
        $aVal = $this->get('aVal');
        $aVal = explode(',', $aVal);
        $iQuestionId = $aVal[0];
        $iAnswerId = $aVal[1];
        $bUp = (int)$aVal[2];

        $aVal = array(
            'question_id' => $iQuestionId,
            'answer_id' => $iAnswerId,
            'up' => $bUp,
            'user_id' => Phpfox::getUserId(),
            'action' => 3
        );
        $aCondition = array(
            'answer_id = ' . $iAnswerId,
            'up = ' . (int)$bUp,
            'user_id = ' . Phpfox::getUserId(),
            'action = 3'
        );
        $aPoint = Phpfox::getService('answer.point')->getListNoPage('*', $aCondition);
        if (count($aPoint['data']) == 0) {
            Phpfox::getService('answer.point')->saveDatabase($aVal);
            if ($bUp) {
                Phpfox::getService('feed.process')->add('answer_voteup', $iAnswerId);
                $aVote = Phpfox::getService('answer.point')->getVote($iAnswerId);
                $sUp = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',1;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/voteup.gif"> </a> <span>'. $aVote['voteUp'] .'</span>';
                $sDown = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',0;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/votedownde.gif"></a> <span>'. $aVote['voteDown'] .'</span>';
            }
            else {
                Phpfox::getService('feed.process')->add('answer_votedown', $iAnswerId);
                $aVote = Phpfox::getService('answer.point')->getVote($iAnswerId);
                $sUp = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',1;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/voteupde.gif"></a> <span>'. $aVote['voteUp'] .'</span>';
                $sDown = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',0;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/votedown.gif"> </a><span>'. $aVote['voteDown'] .'</span>';
            }

            $this->html('#voteup_' . (int)$iAnswerId, $sUp);
            $this->html('#votedown_' . (int)$iAnswerId, $sDown);
            $this->alert(Phpfox::getPhrase('answer.vote_successfully'));
        } else {
            Phpfox::getService('answer.point')->delete($aPoint['data'][0]['id']);
            $aVote = Phpfox::getService('answer.point')->getVote($iAnswerId);
            $sUp = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',1;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/voteupde.gif"></a> <span>'. $aVote['voteUp'] .'</span>';
            $sDown = '<a href="#" onclick="$.ajaxCall(\'answer.vote\',\'aVal='. $iQuestionId .','. $iAnswerId .',0;\' ); return false;"><img src="'. Phpfox::getLib('url')->makeUrl('') .'module/answer/static/image/votedownde.gif"></a> <span>'. $aVote['voteDown'] .'</span>';
            $this->html('#voteup_' . (int)$iAnswerId, $sUp);
            $this->html('#votedown_' . (int)$iAnswerId, $sDown);
            $this->alert(Phpfox::getPhrase('answer.you_voted_it'));
        }
    }

    public function upgradeFeatured()
    {
        $iQuestionId = $this->get('id');
        $aCondition = array('q.id = ' . $iQuestionId);
        $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
        $aQuestion = $aQuestion['data'];
        $aVal['id'] = $iQuestionId;
        $aVal['featured'] = !$aQuestion['featured'];
        Phpfox::getService('answer.question')->saveDatabase($aVal);
        if ($aVal['featured']) {
            $this->alert(Phpfox::getPhrase('answer.this_question_is_featured'));
            $sStatus ='<div class="featured">'.Phpfox::getPhrase('answer.featured')  .'</div>';
            $sFeatured ='<a href="#" onclick="$.ajaxCall(\'answer.upgradeFeatured\', \'id='. $iQuestionId .'\', \'GET\'); return false;">' . Phpfox::getPhrase('answer.unfeatured') .'</a>';
        }
        else {
            if ($aQuestion['status'] == 1) {
                $sStatus ='<div class="resolved">'.Phpfox::getPhrase('answer.resolved')  .'</div>';
            }
            else if ($aQuestion['status'] == 2) {
                $sStatus ='<div class="closed">'.Phpfox::getPhrase('answer.closed')  .'</div>';
            }
            else {
                $sStatus = "";
            }
            $sFeatured = $sFeatured ='<a href="#" onclick="$.ajaxCall(\'answer.upgradeFeatured\', \'id='. $iQuestionId .'\', \'GET\'); return false;">' . Phpfox::getPhrase('answer.featured') .'</a>';
        }
        $this->html('.status', $sStatus);
        $this->html('.featured_status', $sFeatured);
    }

    public function closed()
    {
        $iQuestionId = $this->get('id');
        $aCondition = array('q.id = ' . $iQuestionId);
        $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
        $aQuestion = $aQuestion['data'];
        $aVal['id'] = $iQuestionId;
        if ($aQuestion['status'] != 2) {
            $aVal['status'] = 2;
            $sClosed ='<a href="#" onclick="$.ajaxCall(\'answer.closed\', \'id='. $iQuestionId .'\', \'GET\'); return false;">' . Phpfox::getPhrase('answer.open') .'</a>';
        }
        else {
            if (Phpfox::getService('answer.point')->getCorrectAnswer($iQuestionId) > 0) {
                $aVal['status'] = 1;
            }
            else {
                $aVal['status'] = 0;
            }
            $sClosed ='<a href="#" onclick="$.ajaxCall(\'answer.closed\', \'id='. $iQuestionId .'\', \'GET\'); return false;">' . Phpfox::getPhrase('answer.closed') .'</a>';
        }

        Phpfox::getService('answer.question')->saveDatabase($aVal);
        $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
        $aQuestion = $aQuestion['data'];
        if ($aQuestion['featured']) {
            $sStatus ='<div class="featured">'.Phpfox::getPhrase('answer.featured')  .'</div>';
        }
        else {
            if ($aQuestion['status'] == 1) {
                $sStatus ='<div class="resolved">'.Phpfox::getPhrase('answer.resolved')  .'</div>';

            }
            else if ($aQuestion['status'] == 2) {
                $sStatus ='<div class="closed">'.Phpfox::getPhrase('answer.closed')  .'</div>';
            }
            else {
                $sStatus = "";
            }
        }
        $this->html('.status', $sStatus);
        $this->html('.closed_status', $sClosed);
    }

    public function showSubCategory()
    {
        $iCategoryId = $this->get('id');
        $aCondition = array('c.parent_id = ' . $iCategoryId);

        $aSubCategories = Phpfox::getService('answer.category')->getListNoPage('c.id, c.name', $aCondition);
        $sResult = "";
        if ((count($aSubCategories['data']) > 0) && ($iCategoryId > 0)) {
            $sResult = "<select name='SubCategory' class='SubCategory'>\n";
            $sResult .= "<option value=0>" . Phpfox::getPhrase('answer.choose_sub_categories') . "</option>\n";
            foreach ($aSubCategories['data'] as $aVal) {
                $sResult .= "<option value=" . $aVal['id'] . ">" . $aVal['name'] . "</option>\n";
            }
            $sResult .= "</select>";
        }
        $this->html('#subcategory', $sResult);

    }
    public function addlike()
    {
        Phpfox::isUser(true);

        if (Phpfox::getService('like.process')->add($this->get('type_id'), $this->get('item_id')))
        {
            $this->template()->getTemplate('answer.block.display');
            $this->html('#js_like_body_p'. $this->get('parent_id'), $this->getContent(false));
            $this->template()->getTemplate('answer.block.display');
            $this->html('#js_like_body_r'. $this->get('parent_id'), $this->getContent(false));
            $this->template()->getTemplate('answer.block.display');
            $this->html('#js_like_body_'. $this->get('parent_id'), $this->getContent(false));
        }
    }
	
	public function checkPoint(){
		if($_SESSION['point']>0){
			echo  $_SESSION['point'];
		}else{
			echo 0;
		}
	}
	public function getRandomQuestion(){		
		$ignoreId = $this->get('ignoreId');
		$result = $this->get('result');
		$rand = rand(1,99999);
		if($rand%2==0){
			$random = 1;
		}else{
			$random = 2;
		}
		if($result=='1'){
			if($_SESSION['point']>0){
				$_SESSION['point'] = $_SESSION['point'] + 1;
			}else{
				$_SESSION['point'] = 1;
			}
		}
		
		$result = Phpfox::getService('answer.question')->getRandomQuestion($ignoreId);
		echo $result[0]['id']."!1-".$result[0]['title']."!1-/file/pic/answer/".$result[0]['image'].",".$result[1]['id']."!1-".$result[1]['title']."!1-/file/pic/answer/".$result[1]['image']."!2-".$random."-".$_SESSION['point'];	
	}
	public function endGame(){
		$point = $_SESSION['point'];
		if(Phpfox::isUser())
		Phpfox::getService('answer.question')->updatePoint(Phpfox::getUserId(),$point);
		echo $point;
		unset($_SESSION['point']); 
		exit();
	}
	public function checkResult()
    {		
		$iId = $this->get('id');
		$result = Phpfox::getService('answer.question')->getAnswer($iId);
		$type = 0;
		if($this->get('type')=='B'){
			$type = 1;
		}				
		if($result['is_ab']==$type){
			if($_SESSION['point']>0){
				$_SESSION['point'] = $_SESSION['point'] + 1;
			}else{
				$_SESSION['point'] = 1;
			}
			if($this->get('end')=='1'){
				$point = $_SESSION['point'];
				if(Phpfox::isUser())
				Phpfox::getService('answer.question')->updatePoint(Phpfox::getUserId(),$point);
				$this->alert(Phpfox::getPhrase('answer.completegame').' '. $point.' '.Phpfox::getPhrase('answer.pointgame').'.' );
				exit();
			}else{ 
				echo '1-'.$_SESSION['point'];
			}
		}else{
			if($this->get('end')=='1'){
				$point = $_SESSION['point'];
				if(Phpfox::isUser())
				Phpfox::getService('answer.question')->updatePoint(Phpfox::getUserId(),$point);
				$this->alert(Phpfox::getPhrase('answer.completegame').' '. $point.' '.Phpfox::getPhrase('answer.pointgame').'.' );
				exit();
			}else{	
				echo '0-'.$_SESSION['point'];
			}
		}
		
		
    }
	
    public function removelike()
    {
        Phpfox::isUser(true);
        if (Phpfox::getService('like.process')->delete($this->get('type_id'), $this->get('item_id')))
        {
            $this->html('#js_like_body_p'. $this->get('parent_id'),"");
            $this->html('#js_like_body_r'. $this->get('parent_id'), "");
            $this->html('#js_like_body_'. $this->get('parent_id'), "");
        }

    }

}